package com.gs.spider.tq;

import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.CharEncoding;
import org.apache.http.NameValuePair;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.utils.HttpConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by TQ on 2017/5/24.
 */
public class ERPTable {

    public void buildReq() {
        Request request = new Request("http://example.com/item");
        request.setMethod(HttpConstant.Method.POST);
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("", ""));

        request.putExtra("nameValuePair", params);
        //Spider.addRequest(request);
        HttpClientUtil client = new HttpClientUtil();
        //client.doPost()
    }

    public static void main(String[] args) {
        ERPTable et = new ERPTable();
        et.test("http://120.40.103.185:8899/login.do");
        //et.getJyfkBlack(null);
    }

    public void test(String url) {
        String result = null;
        HttpClientUtil client = new HttpClientUtil();
        CloseableHttpClient httpClient = client.getHttpClient();
        Map<String, String> formData = new HashMap<>();
        formData.put("userId", "5150262");
        formData.put("pass", "xxfxxf");
        result = HttpUtil.doGet(url, "UTF-8", httpClient);
        JXDocument doc = new JXDocument(result);
        try {
            String validate = doc.selN("//input[@name='validate']/@value").get(0).getTextVal();
            formData.put("validate", validate);
            String res = client.login(url, formData, httpClient);
            System.out.println(res);


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    public void getJyfkBlack(String url) {
        String result = null;
        url = "http://www.zuchefk.com/login.aspx";
        HttpClientUtil client = new HttpClientUtil();
        CloseableHttpClient httpClient = client.getHttpClient();
        Map<String, String> formData = new HashMap<String, String>();
        JSONObject queryFormData = new JSONObject();
        formData.put("name", "福建喜相逢");
        formData.put("pwd", "xxf123456");
        queryFormData.put("sqlWhere", "");
        queryFormData.put("cid", "");
        result = HttpUtil.doGet(url, "UTF-8", httpClient);
        JXDocument doc = new JXDocument(result);
        try {
            String viewState = doc.selN("//input[@id='__VIEWSTATE']/@value").get(0).getTextVal();
            String viewStateGenerator = doc.selN("//input[@id='__VIEWSTATEGENERATOR']/@value").get(0).getTextVal();
            String eventValidation = doc.selN("//input[@id='__EVENTVALIDATION']/@value").get(0).getTextVal();
            String buttonOK = doc.selN("//input[@id='ButtonOK']/@value").get(0).getTextVal();
            formData.put("__VIEWSTATE", viewState);
            formData.put("__VIEWSTATEGENERATOR", viewStateGenerator);
            formData.put("__EVENTVALIDATION", eventValidation);
            formData.put("ButtonOK", buttonOK);
            result = HttpUtil.doPost(url, "UTF-8", formData, httpClient);
            result = HttpUtil.doPost(url, "UTF-8", queryFormData, httpClient);
            JSONObject resultObj = JSON.parseObject(result).getJSONObject("d");
           /* black.setCertNo(certNo);
            black.setName(name);
            black.setErrTotal(resultObj.getInteger("Total"));
            black.setErrStr(resultObj.getString("errStr"));
            black.setName(name);
            black.setFname(resultObj.getString("fname"));
            black.setResource("5");*/
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}
